package com.lzy.okgo.harmony.http;

import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.harmony.model.HomeArticleModel;
import com.lzy.okgo.harmony.model.WxArticleModel;
import com.lzy.okgo.harmony.model.base.HttpModel;
import com.lzy.okgo.harmony.model.base.PageModel;
import com.lzy.okgo.request.GetRequest;
import com.lzy.okrx2.adapter.ObservableBody;
import io.reactivex.Observable;

import java.lang.reflect.Type;
import java.util.List;

/**
 * 玩安卓接口请求封装
 */
public class WanRequester {
    private WanRequester() {
    }

    /**
     * 获取首页文章列表
     *
     * @param page 页码，从0开始
     */
    public static Observable<HttpModel<PageModel<HomeArticleModel>>> getHomeArticle(String tag, int page) {
        String url = "https://www.wanandroid.com/article/list/" + page + "/json";
        Type type = new TypeToken<HttpModel<PageModel<HomeArticleModel>>>() {
        }.getType();
        GetRequest<HttpModel<PageModel<HomeArticleModel>>> request = OkGo.get(url);
        return request
                .tag(tag)
                .converter(new ModelConvert<>(type))
                .adapt(new ObservableBody<>());
    }

    /**
     * 获取公众号列表
     */
    public static Observable<HttpModel<List<WxArticleModel>>> getWxArticle(String tag) {
        String url = "https://wanandroid.com/wxarticle/chapters/json";
        Type type = new TypeToken<HttpModel<List<WxArticleModel>>>() {
        }.getType();
        GetRequest<HttpModel<List<WxArticleModel>>> request = OkGo.get(url);
        return request
                .tag(tag)
                .converter(new ModelConvert<>(type))
                .adapt(new ObservableBody<>());
    }
}